package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.ChatGroup;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.VO.ChatGroupQueryVO;
import org.wmq.travelagency02.service.ChatGroupService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/chat-group")
@RequiredArgsConstructor
public class ChatGroupController {


    private final ChatGroupService chatGroupService;

    @GetMapping("/{id}")
    public Result<ChatGroupQueryVO> getById(@PathVariable String id){
        ChatGroupQueryVO result = this.chatGroupService.getChatGroupQueryVOById(id);
        if (result == null){
            return Result.error("没有找到该数据");
        }
        return Result.ok(result);
    }

    @PostMapping
    public Result<Void> save(@RequestBody ChatGroup chatGroup){
        chatGroup.setCreateTime(LocalDateTime.now());
        chatGroup.setUpdateTime(LocalDateTime.now());
        boolean success = this.chatGroupService.save(chatGroup);
        if (success){
            return Result.ok();
        }
        return Result.error("保存失败");
    }

    @PutMapping
    public Result<Void> update(@RequestBody ChatGroup chatGroup){
        chatGroup.setUpdateTime(LocalDateTime.now());
        boolean success = this.chatGroupService.updateById(chatGroup);
        if (success){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable String id){
        boolean success = this.chatGroupService.removeChatGroupById(id);
        if (success){
            return Result.ok();
        }
        return Result.error("删除失败");
    }
}
